package com.jorado.redis;

import com.jorado.core.statistics.builder.BuilderManager;
import com.jorado.core.statistics.builder.RedisStatisticsServiceBuilder;
import com.jorado.core.statistics.model.RedisStatistics;

import java.util.Set;

public class RedisStatisticsHandler {

    public void statistics(String ipAddress, String command, String result, long costTime) {
        RedisStatistics redisStatistics = this.createRedisStatistics(ipAddress, command, result, costTime);
        Set<RedisStatisticsServiceBuilder> builders = BuilderManager.instance().getRedisStatisticsServiceBuilders();
        for (RedisStatisticsServiceBuilder builder : builders) {
            builder.build().statistics(redisStatistics);
        }
    }

    private RedisStatistics createRedisStatistics(String ipAddress, String command, String status,
                                                  long costTime) {
        RedisStatistics redisStatistics = new RedisStatistics();
        redisStatistics.setServerIp(ipAddress);
        redisStatistics.setCommand(command);
        redisStatistics.setStatus(convertStatus(status));
        redisStatistics.setCostTime(costTime);
        return redisStatistics;
    }

    private String convertStatus(String result) {
        return result;
    }
}
